package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import school.domain.Person;
import school.mapper.PersonMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("person")
public class PersonController extends BaseController {


    @Autowired
    private PersonMapper personMapper;

    /**
     * 信用卡查询
     *
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("cardList")
    public String cardList(String cardlevel, String cardstatus,
                           @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String phone, @RequestParam(defaultValue = "") String card,
                           @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(cardlevel)) {
            criteria.andEqualTo("cardlevel", cardlevel);
        }
        if (!StringUtils.isEmpty(cardstatus)) {
            criteria.andEqualTo("cardstatus", cardstatus);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(card)) {
            criteria.andEqualTo("card", card);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andEqualTo("phone", phone);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Person> pageInfo = new PageInfo<>(personMapper.selectByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "person/list-card";
    }

    /**
     * 公积金查询
     *
     * @param gjjlevel
     * @param gjjdstatus
     * @param name
     * @param phone
     * @param card
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("gjjList")
    public String gjjList(String gjjlevel, String gjjdstatus,
                          @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String phone, @RequestParam(defaultValue = "") String card,
                          @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(gjjlevel)) {
            criteria.andEqualTo("gjjlevel", gjjlevel);
        }
        if (!StringUtils.isEmpty(gjjdstatus)) {
            criteria.andEqualTo("gjjdstatus", gjjdstatus);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(card)) {
            criteria.andEqualTo("card", card);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andEqualTo("phone", phone);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Person> pageInfo = new PageInfo<>(personMapper.selectByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "person/list-gjj";
    }

    @RequestMapping("dkList")
    public String dkList(String dklevel, String dkstatus,
                         @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String phone, @RequestParam(defaultValue = "") String card,
                         @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(dklevel)) {
            criteria.andEqualTo("dklevel", dklevel);
        }
        if (!StringUtils.isEmpty(dkstatus)) {
            criteria.andEqualTo("dkstatus", dkstatus);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(card)) {
            criteria.andEqualTo("card", card);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andEqualTo("phone", phone);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Person> pageInfo = new PageInfo<>(personMapper.selectByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "person/list-dk";
    }

    @RequestMapping("zfbList")
    public String zfbList(String zfblevel, String zfbstatus,
                          @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String phone, @RequestParam(defaultValue = "") String card,
                          @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(zfblevel)) {
            criteria.andEqualTo("zfblevel", zfblevel);
        }
        if (!StringUtils.isEmpty(zfbstatus)) {
            criteria.andEqualTo("zfbstatus", zfbstatus);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(card)) {
            criteria.andEqualTo("card", card);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andEqualTo("phone", phone);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Person> pageInfo = new PageInfo<>(personMapper.selectByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "person/list-zfb";
    }


    @RequestMapping("zhList")
    public String zhList(String[] statu,
                         @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String phone, @RequestParam(defaultValue = "") String card,
                         @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        String status = "";

        if (statu != null && statu.length > 0) {
            for (String s : statu) status += s;
        }
        /*if (status.contains("card")) {
            criteria.andNotEqualTo("cardstatus", "未逾期");
        }
        if (status.contains("dk")) {
            criteria.andNotEqualTo("dkstatus", "未失信");
        }
        if (status.contains("gjj")) {
            criteria.andNotEqualTo("gjjstatus", "未失信");
        }
        if (status.contains("zfb")) {
            criteria.andNotEqualTo("zfbstatus", "未失信");
        }*/

        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(card)) {
            criteria.andEqualTo("card", card);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andEqualTo("phone", phone);
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Person> pageInfo = new PageInfo<>(personMapper.selectByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("status",status);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "person/list-zh";
    }

    @RequestMapping("person")
    public String person(@RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "") String phone, @RequestParam(defaultValue = "") String card, Model model) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andEqualTo("name", name);
        }
        if (!StringUtils.isEmpty(card)) {
            criteria.andEqualTo("card", card);
        }
        if (!StringUtils.isEmpty(phone)) {
            criteria.andEqualTo("phone", phone);
        }
        List<Person> list = personMapper.selectByExample(example);
        if(list.size() ==1 ){
            model.addAttribute("person",list.get(0));
        }

        return "person/list-person";
    }

    /**
     * 详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{type}/{id}")
    public String detail(@PathVariable Integer id, @PathVariable String type, Model model) {
        Person person = personMapper.selectByPrimaryKey(id);
        model.addAttribute(person);
        model.addAttribute("type", type);
        return "person/detail";
    }

    /**
     * 修改信息
     *
     * @param person
     * @return
     */
    @RequestMapping("update")
    public String update(Person person) {
        personMapper.updateByPrimaryKeySelective(person);
        person = personMapper.selectByPrimaryKey(person.getId());
        countPerson(person);
        personMapper.updateByPrimaryKeySelective(person);
        return refresh();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        personMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 个人
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("person/{id}")
    public String person(@PathVariable Integer id,Model model){
        Person person = personMapper.selectByPrimaryKey(id);
        model.addAttribute(person);
        return "person/person";
    }


    @ResponseBody
    @RequestMapping("export")
    public String export() {
        String[] cardLevel = {"无限卡", "白金卡", "金卡", "普通卡"};
        String[] cardStatus = {"已逾期", "未逾期"};
        String[] gjjlevel = {"C级", "B级", "BB级", "BBB级", "A级", "AA级", "AAA级"};
        String[] gjjstatus = {"严重失信", "一般失信", "未失信"};
        String[] dklevel = {"无贷款", "有贷款-正常", "有贷款-关注", "有贷款-次级", "有贷款-可疑", "有贷款-损失"};
        String[] dkstatus = {"失信", "未失信"};
        String[] zfblevel = {"700-950", "650-700", "600-650", "550-600", "350-550"};
        for (int i = 1; i < 1000; i++) {
            Person person = new Person();
            person.setName("用户" + i);
            person.setCard((1589652389 + UUID.randomUUID().hashCode()) + "");
            person.setPhone((135230545 + UUID.randomUUID().hashCode()) + "");
            person.setCardlevel(cardLevel[i % 4]);
            person.setCardstatus(cardStatus[i % 2]);
            person.setGjjstatus(gjjstatus[i % 3]);
            person.setGjjlevel(gjjlevel[i % 3]);
            person.setDklevel(dklevel[i % 6]);
            person.setDkstatus(dkstatus[i % 2]);
            person.setZfblevel(zfblevel[i % 5]);
            person.setZfbstatus(dkstatus[i % 2]);
            countPerson(person);
            personMapper.insert(person);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("u")
    public String u() {
        List<Person> list = personMapper.selectAll();
        for (Person person : list) {
            person.setUnit("单位" + person.getId());
            person.setCard(person.getCard().replace("-", ""));
            person.setPhone(person.getPhone().replace("-", ""));
            personMapper.updateByPrimaryKey(person);
        }
        return "success";
    }


    private void countPerson(Person person) {
        int p = 0;
        if (!person.getCardstatus().equals("未逾期")) p++;
        if (!person.getGjjstatus().equals("未失信")) p++;
        if (!person.getDkstatus().equals("未失信")) p++;
        if (!person.getZfbstatus().equals("未失信")) p++;
        person.setPerson(p);
    }
}
